/*
 * @Author: szx
 * @Date: 2022-04-19 19:07:48
 * @LastEditTime: 2022-04-19 19:19:45
 * @Description:
 * @FilePath: \leetcode\剑指offerII\073\073.js
 */
/**
 * @param {number[]} piles
 * @param {number} h
 * @return {number}
 */
var minEatingSpeed = function (piles, h) {
    let l = 0,
        r = Math.max(...piles);
    while (l <= r) {
        const mid = l + ((r - l) >> 1);
        if (canFinish(piles, mid, h)) {
            r = mid - 1;
        } else left = mid + 1;
    }
    return l;
};
function canFinish(piles, speed, h) {
    const timeOf = (n, speed) => {
        return Math.floor(n / speed) + (n % speed > 0);
    };
    let time = 0;
    for (let a of piles) {
        time += timeOf(a, speed);
    }
    return time <= h;
}
